import { Component, OnInit } from "@angular/core";
import { NzNotificationService, UploadFile } from "ng-zorro-antd";
import { Router } from "@angular/router";
import { HttpServcie } from "../../../service/http.service";
import { Config } from "../../../service/config";

@Component({
  selector: "self-count-page",
  templateUrl: "self-count.html",
  styleUrls: ["self-count.scss"]
})
export class SelfCountPage implements OnInit {
  constructor(
    private httpServcie: HttpServcie,
    private router: Router,
    private _notification: NzNotificationService
  ) { }
  config: any = Config;
  //接收参数
  userState = Config.USER_STATE;
  //返回的集合
  list: any = [];
  //总页数
  total: string;
  //多选框
  allChecked: any = false;
  //多选框
  indeterminate: any = false;
  partySeleList = [];
  toParty: any;
  //设置add的
  isAddVisible = false;
  isAddConfirmLoading = false;
  inValue: string;
  isConfirmLoading = false;
  totalList: any = [];
  searchOptions
  titleList: any = [];
  sqlstring: string = '';
  autosize = {
    minRows: 2,
    maxRows: 16
  };
  ngOnInit() {

  }

  //查询
  query() {
    if (!this.sqlstring) {
      return;
    }
    let param = {
      sqlstring: this.sqlstring
    }
    this.httpServcie.selfCount(param).subscribe(
      data => {
        if (data.status === 200) {
          this.list = data.data.list;

          let list2 = [];
          let titleList2 = [];
          if (data.data.list) {
            this.total = data.data.list.length;
            let obj2 = data.data.list[0];
            for (let key in obj2) {
              titleList2.push(key)
            }
            this.titleList = titleList2;

            for (let i = 0; i < data.data.list.length; i++) {
              let obj = data.data.list[i];
              let list3 = [];
              for (let key in obj) {
                list3.push(obj[key])
              }
              list2.push(list3);
            }
            this.list = list2;


          } else {
            this.total = '0';
          }

          console.log(data);
        } else if (data.status === 401) {
          this.router.navigateByUrl("/login");
          return;
        } else {
          // 异常
          this._notification.create("error", "错误提示", data.message);
        }
      },
      error => {
        console.log(JSON.stringify(error));
      }
    );
  }

  // 查询面板
  panels = {
    active: true,
    name: "查询",
    disabled: false,
    childPanel: [
      {
        active: false,
        name: "查询"
      }
    ]
  };

  //table加载数据时
  displayDataChange($event) {
    this.list = $event;
    this.refreshStatus();
  }

  //重置选择框
  refreshStatus() {
    const allChecked = this.list.every(
      value => value.disabled || value.checked
    );
    const allUnChecked = this.list.every(
      value => value.disabled || !value.checked
    );
    this.allChecked = allChecked;
    this.indeterminate = !allChecked && !allUnChecked;
  }

  //全选
  checkAll(value) {
    if (value) {
      this.list.forEach(data => {
        if (!data.disabled) {
          data.checked = true;
        }
      });
    } else {
      this.list.forEach(data => (data.checked = false));
    }
  }

  //分页
  nzPageSizeChange(event) {
    this.query();
  }

  //输入框的方法
  nzPageIndexChange(event) {

  }

  //当前页
  nzPageIndexClickChange(event) {

    this.query();
  }

  download(url, name) {
    let link = document.createElement("a");
    let body = document.querySelector("body");

    this.getBlob(this.config.IP + this.config.API.getUpload + url).then(
      blob => {
        link.href = window.URL.createObjectURL(blob);
        link.download = name;
        // fix Firefox
        link.style.display = "none";
        body.appendChild(link);
        link.click();
        body.removeChild(link);
        window.URL.revokeObjectURL(link.href);
      }
    );
  }
  getBlob(url) {
    return new Promise(resolve => {
      const xhr = new XMLHttpRequest();
      xhr.open("GET", url, true);
      xhr.responseType = "blob";
      xhr.onload = () => {
        if (xhr.status === 200) {
          resolve(xhr.response);
        }
      };
      xhr.send();
    });
  }
}
